package com.sheng.project.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.sheng.project.utils.dto.WeekArrayDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liusheng
 * @date 2022/09/07
 */
public class WeekUtil {

    public static List<WeekArrayDTO> getWeekListByMonth(String date) {
        DateTime parse = DateUtil.parse(date, "yyyy-MM");
        DateTime beginOfMonth = DateUtil.beginOfMonth(parse);
        DateTime endOfMonth = DateUtil.endOfMonth(parse);

        DateTime index = beginOfMonth;
        List<WeekArrayDTO> weekList = new ArrayList<>();
        while (index.isBefore(endOfMonth)) {
            DateTime beginOfWeek = DateUtil.beginOfWeek(index);
            String begin = beginOfWeek.toString("yyyy-MM-dd");
            DateTime endOfWeek = DateUtil.endOfWeek(index);
            String end = endOfWeek.toString("yyyy-MM-dd");
            WeekArrayDTO weekArrayDTO = new WeekArrayDTO(begin, end);
            weekList.add(weekArrayDTO);
            index = DateUtil.offsetDay(index, 7);
        }
        return weekList;
    }

    public static List<WeekArrayDTO> getWeekListByYear(String year) {
        DateTime parse = DateUtil.parse(year, "yyyy");
        DateTime beginOfYear = DateUtil.beginOfYear(parse);
        DateTime endOfYear = DateUtil.endOfYear(parse);

        DateTime index = beginOfYear;
        List<WeekArrayDTO> weekList = new ArrayList<>();
        while (index.isBefore(endOfYear)) {
            DateTime beginOfWeek = DateUtil.beginOfWeek(index);
            String begin = beginOfWeek.toString("yyyy-MM-dd");
            DateTime endOfWeek = DateUtil.endOfWeek(index);
            String end = endOfWeek.toString("yyyy-MM-dd");
            WeekArrayDTO weekArrayDTO = new WeekArrayDTO(begin, end);
            weekList.add(weekArrayDTO);
            index = DateUtil.offsetDay(index, 7);
        }
        return weekList;
    }
}

